package us.yesor.atlast;

import us.yesor.atlast.experimental.SampleDataLoader;
import us.yesor.atlast.framework.ContentProviderAdapter;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.format.Time;
import android.view.View;
import android.widget.ListView;

public class ViewItemsActivity extends ListActivity {

	private SimpleCursorAdapter adapter;
	private String activeCollection;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		
		SharedPreferences sp = getSharedPreferences("prefs", Context.MODE_PRIVATE);

		activeCollection = sp.getString("activeCollection", "No Collection Chosen");
		if (!activeCollection.equals("No Collection Chosen")){
			//TODO Verify with user they would like to see items from the active collection
		} else {
			Intent myIntent = new Intent(this, ViewCollectionsActivity.class);
			startActivity(myIntent);
		}
		
		//TODO remove this before production
		//SampleDataLoader.loadSampleData(this);
		
		String[] projections = new String[] {InteractionTable.IID,
				InteractionTable.ITEM, InteractionTable.STATE, InteractionTable.TIME};
		
		String selection = InteractionTable.COLLECTION+" = ?";
		
		String[] args = new String[] {activeCollection};
		
		Cursor c = getContentResolver().query(InteractionTable.CONTENT_URI, projections,
				selection, args, null);
		startManagingCursor(c);
		
		String[] columns = new String[] {InteractionTable.ITEM, InteractionTable.STATE, InteractionTable.TIME};
		
		int[] to = new int[] {R.id.name_entry, R.id.state_entry, R.id.time_entry};
		
		//TODO deprecated SimpleCursorAdapter
		adapter = new SimpleCursorAdapter(this, R.layout.item_entry, c, columns, to);
		
		this.setListAdapter(adapter);
	}
	
	//TODO what to do when row selected
	// Shown is to create a new interaction at the current time
	// Instead, bring up a "state chooser" dialog or offer to insert a new state
//	@Override
//	protected void onListItemClick(ListView l, View v, int position, long id) {
//		super.onListItemClick(l, v, position, id);
//		Cursor c = (Cursor) adapter.getItem(position);
//		String item = c.getString(1);
//		String state = c.getString(2);
//		String trigger = "Manual";
//		String value = item;
//		String collection = activeCollection;
//		Time time = new Time();
//		time.setToNow();
//		String timeString = time.format3339(false);
//		ItemInteraction i = new ItemInteraction(collection, item, trigger, value, state, timeString);
//		getContentResolver().insert(InteractionTable.CONTENT_URI,ContentProviderAdapter.getFormattedContentValues(i));
//	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Cursor c = (Cursor) adapter.getItem(position);
		SharedPreferences sp = getSharedPreferences("prefs", Context.MODE_PRIVATE);
		int column = c.getColumnIndex(InteractionTable.ITEM);
		Editor e = sp.edit();
		e.putString("activeItem", c.getString(column));
		e.commit();
		Intent myIntent = new Intent(this, ViewHistoryActivity.class);
		startActivity(myIntent);
	}
	
}
